<?php
namespace app\index\model;

use think\Model;
use think\Db;
class Employee extends Model
{
    protected $table = 'employee';
    //自定义初始化
    protected function initialize()
    {
        //需要调用`Model`的`initialize`方法
        parent::initialize();
        //TODO:自定义的初始化
    }
   
   public function get_employ_count($where){
   		$arr= Db::table($this->table)->alias("e")
   		->join("department_employee de","e.id=de.employee_id","LEFT")
   		->join("department d","de.department_id=d.id","LEFT")
   		->join("users u","e.id=u.employee_id","LEFT")
   		->join("users_role ur","u.id=ur.users_id","LEFT")
   		->join("role r","ur.role_id=r.id","LEFT")
   		->join("users_role_group urg","u.id=urg.users_id","LEFT")
   		->join("role_group rg","urg.role_group_id=rg.id","LEFT")
   		->join("job_employee je","e.id=je.employee_id","LEFT")
   		->join("job j","je.job_id=j.id","LEFT")
   		->where($where)
   		->distinct("e.id")
   		->field("e.id")
   		->select();
   		return count($arr);
   }
   public function get_eid_list($where,$page,$lists){
   		$pagestart=($page-1)*$lists;
   		$arr= Db::table($this->table)->alias("e")
   		->join("department_employee de","e.id=de.employee_id","LEFT")
   		->join("department d","de.department_id=d.id","LEFT")
   		->join("users u","e.id=u.employee_id","LEFT")
   		->join("users_role ur","u.id=ur.users_id","LEFT")
   		->join("role r","ur.role_id=r.id","LEFT")
   		->join("users_role_group urg","u.id=urg.users_id","LEFT")
   		->join("role_group rg","urg.role_group_id=rg.id","LEFT")
   		->join("job_employee je","e.id=je.employee_id","LEFT")
   		->join("job j","je.job_id=j.id","LEFT")
   		->where($where)
   		->order("e.id")
   		->distinct("e.id")
   		->field("e.id")
   		->limit($pagestart,$lists)
   		->select();
   		$res_Arr=[];
   		foreach ($arr as $k => $v) {
   			$res_Arr[]=$v["id"];
   		}
   		return $res_Arr;
   }
   //查询员工信息
   public function get_employ($eids){
   		$arr= Db::table($this->table)->alias("e")
   		->join("department_employee de","e.id=de.employee_id","LEFT")
   		->join("department d","de.department_id=d.id","LEFT")
   		->join("users u","e.id=u.employee_id","LEFT")
   		->where("e.is_actived=1 and e.is_deleted=0 and ((de.index=1 and d.is_actived=1 and d.is_deleted=0) or de.index is null) and e.id in({$eids})")
   		->order("e.id")
   		->field("e.id,e.em_name,u.login_name,d.de_name,d.id as de_id,e.idcard,e.mobile,e.state,u.id as uid")
   		->select();
   		return $arr;
   }
   //查询员工角色
   public function get_role_arr($eids){
   	$arr=Db::table($this->table)->alias("e")
   	    ->join("users u","e.id=u.employee_id","LEFT")
   		->join("users_role ur","u.id=ur.users_id","LEFT")
   		->join("role r","ur.role_id=r.id","LEFT")
   		->where("r.is_actived=1 and r.is_deleted=0 and e.id in({$eids})")
   		->field("e.id as em_id,r.role_name,r.id as role_id")
   		->select();
   		return $arr;
   }
   //查询员工角色组
	public function get_role_group_arr($eids){
   	$arr=Db::table($this->table)->alias("e")
   	    ->join("users u","e.id=u.employee_id","LEFT")
   		->join("users_role_group urg","u.id=urg.users_id","LEFT")
   		->join("role_group rg","urg.role_group_id=rg.id","LEFT")
   		->where("rg.is_actived=1 and rg.is_deleted=0 and e.id in({$eids})")
   		->field("e.id as em_id,rg.group_name,rg.id as group_id")
   		->select();
   		return $arr;
   }
   //查询员工岗位
   public function get_job_list($eids){
   		$arr=Db::table($this->table)->alias("e")
   		->join("job_employee je","e.id=je.employee_id","LEFT")
   		->join("job j","je.job_id=j.id","LEFT")
   		->where("j.is_actived=1 and j.is_deleted=0 and e.id in({$eids})")
   		->field("e.id as em_id,j.job_name,j.id as job_id")
   		->select();
   		return $arr;
   }
   
}
    
?>